package com.csun.cmny.mqtt.app;

import com.csun.cmny.mqtt.constant.MqttMsgConstants;
import com.csun.cmny.mqtt.core.DeviceDataBase;
import com.csun.cmny.mqtt.core.MqttClientBase;
import com.csun.cmny.mqtt.entity.*;
import com.csun.cmny.mqtt.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;

import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-15 15:13
 * @Description:
 */
@Slf4j
public class MqttClientPub extends MqttClientBase {

    private static MqttClientPub mqttClientPub = new MqttClientPub();

    public static MqttClientPub getMqttClientPub() {
        return mqttClientPub;
    }

    /**
     * 断线重连
     */
    @Override
    public boolean mqttClientReConn() {
        try {
            mqttClient.connect(options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * @Description: 给多个设备发送指令
     * @param @param 设备sn列表
     * @param @param 要发送的指令
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月11日
     */
    public void publish(List<String> topicList, Object msg) {

        try {
            for (String s : topicList) {
                /* 发送指令 */
                publish(s, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     *
     * @Description: 给单个设备发送指令
     * @param @param 设备sn码
     * @param @param 发送的主题
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月11日
     */
    public void publish(String topic, Object msg) {

        try {
            /* mqtt 主题 */
            MqttTopic mqttTopic = mqttClient.getTopic(topic);
            /* mqtt 消息 */
            MqttMessage mqttMsg = new MqttMessage();
            /* mqtt 消息质量 */
            mqttMsg.setQos(1);
            /* mqtt 消息是否保持 */
            mqttMsg.setRetained(true);
            /* mqtt 消息内容 */
            mqttMsg.setPayload(JsonUtil.getInfoMap(msg).getBytes());
            /* mqtt client 发布消息 */
            publish(mqttTopic, mqttMsg);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 给指定的主题发送消息
     * @param topic 要发送消息的主题 即要发送指令的设备编码
     * @param message 要发送的消息
     * @throws MqttException
     * @throws MqttPersistenceException
     */
    public boolean publish(MqttTopic topic , MqttMessage message)
            throws MqttPersistenceException, MqttException {

        MqttDeliveryToken token = topic.publish(message);
        token.waitForCompletion();

        log.info("publish to topic '" + topic.getName()
                + "' result " + token.isComplete() + " msg = '"
                + new String(message.getPayload()) + "'");

        return token.isComplete();
    }

    /* (non-Javadoc)
     * @see com.csun.mqtt.MqttClientBase#mqttClientStart(int[], java.lang.String[])
     */
    @Override
    public boolean mqttClientStart(int[] qos, String[] topic,
                                   DeviceDataBase deviecData) throws MqttException {
        // 不用实现该方法
        return false;
    }

    /**
     * @Description: 设备开关机授权
     * @param @topicList 设备编码列表
     * @param @isOpenValid 开关机授权
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月11日
     */
    public void msgInviteValid(List<String> topicList, String isOpenValid) {

        MsgInviteValid msgInviteOpenValid =
                new MsgInviteValid(MqttMsgConstants.INVITE_VALID, "", isOpenValid);
        /* 发送指令 */
        //publish(topicList, msgInviteOpenValid);
        for (String sn : topicList) {
            msgInviteOpenValid.setSn(sn);
            /* 发送指令 */
            publish(sn, msgInviteOpenValid);
        }
    }

    /**
     * @Description: 设备开关机
     * @param @topicList 设备编码列表
     * @param @isOpen 开关机
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月11日
     */
    public void msgInviteOpen(List<String> topicList, String isOpen) {

        MsgInviteOpen msgInviteOpen = new MsgInviteOpen(MqttMsgConstants.INVITE_OPEN, "", isOpen);

        for (String sn : topicList) {
            msgInviteOpen.setSn(sn);
            /* 发送指令 */
            publish(sn, msgInviteOpen);
        }

    }

    /**
     * @Description: 设备数据上传
     * @param @topicList 设备编码列表
     * @param @timeFlag 时间戳
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月11日
     */
    public void msgInviteData(List<String> topicList, String timeFlag) {

        MsgInviteData msgInviteData = new MsgInviteData(MqttMsgConstants.INVITE_DATA, "", timeFlag);

        //publish(topicList, msgInviteData);
        for (String sn : topicList) {
            msgInviteData.setSn(sn);
            /* 发送指令 */
            publish(sn, msgInviteData);
        }

    }

    /**
     * @Description: 推送报警
     * @param @topicList 接受报警主题
     * @param @sn 设备sn码
     * @param @alertMsg 报警信息
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月12日
     */
    public void msgInviteAlert(List<String> topicList, String sn,
                               String alertMsg) {

        MsgInviteAlert msgInviteAlert = new MsgInviteAlert(MqttMsgConstants.INVITE_ALERT,
                sn, alertMsg);

        publish(topicList, msgInviteAlert);

    }

    /**
     * @Description: TODO
     * @param @param topicList
     * @param @param uploadTest
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年10月12日
     */
    public void msgInviteUpload(List<String> topicList,
                                MsgInviteUpload upload) {
        // TODO Auto-generated method stub

        upload.setMsgType(MqttMsgConstants.INVITE_UPLOAD);

        publish(topicList, upload);

    }

    /**
     * @Description: TODO
     * @param @param topicList
     * @param @param cmdArm
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年11月1日
     */
    public void msgPhoneCmd(List<String> topicList, String cmd) {
        // TODO Auto-generated method stub
        MsgPhoneCmd msgPhoneCmd =
                new MsgPhoneCmd(MqttMsgConstants.PHONE_CMD, "", cmd);
        /* 发送指令 */
        //publish(topicList, msgInviteOpenValid);
        for (String sn : topicList) {
            msgPhoneCmd.setSn(sn);
            /* 发送指令 */
            publish(sn, msgPhoneCmd);
        }
    }

    /**
     * @Description: TODO
     * @param @param lists
     * @param @param string
     * @return void
     * @throws
     * @author chisj chisj@foxmail.com
     * @date 2017年11月24日
     */
    public void msgDataActual(List<String> topicList, String string) {
        // TODO Auto-generated method stub
        for (String sn : topicList) {
            //msgPhoneCmd.setSn(sn);
            /* 发送指令 */
            publish(sn, string);
        }
    }

}
